package com.hava.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

/**
 * Created by yanfa on 2016/10/29.
 */

@Controller
public class HavaHelloController {

    @RequestMapping(value = "/response")
    public void response(HttpServletResponse response) throws IOException {
        System.out.println(response);
        response.getWriter().write("Hello,Spring Web!!");
    }

    @RequestMapping(value = "/write")
    public void write(Writer writer) throws IOException {
        System.out.println(writer);
       writer.write("Hello,Spring Web!!");
    }

    @RequestMapping(value = "/user/{userId}")
    public void user(@PathVariable("userId") String userId,Writer writer) throws IOException {
        writer.write("userId= " + userId);
    }

    @RequestMapping(value = "/user-host/{userId}")
    public void user_host(@PathVariable("userId") String userId,
                          @RequestParam("msg") String msg,
                          @RequestHeader("host") String host,
                          Writer writer) throws IOException {
        writer.write("userId= " + userId + ", msg=" + msg + ", hostHeader=" + host);
    }

    ////注意如下代码会有输出问题
//    @RequestMapping(value = "/user/login")
//    @ResponseBody
//    public String login(@RequestParam("username") String username,
//                        @RequestParam("password") String password,
//                        @RequestHeader("host") String host,
//                        Writer writer) throws IOException{
//        System.out.println("username:" + username);
//        System.out.println("password:" + password);
//        return "Username: " + username + " " + "Password: " + password + " Host:" + host;
//    }

    @RequestMapping(value = "/user/login")
    @ResponseBody
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestHeader("host") String host) throws IOException{
        System.out.println("username:" + username);
        System.out.println("password:" + password);
        return "<p>Username: " + username + "</p>" + "<p>Password: " + password + "</p>" + "<p>Host:" + host + "</p>";
    }

    @RequestMapping(value = "/user_login/login")
    public String login2(@RequestParam("username") String username,
                         @RequestParam("password") String password,
                         @RequestHeader("host") String host,
                         ModelMap modelMap) throws IOException{
        System.out.println("username:" + username);
        System.out.println("password:" + password);

        // 向ModelMap添加内容
        modelMap.addAttribute("username",username);
        modelMap.addAttribute("password",password);

        //由于没有@ResponseBody，则返回的是View
        return "";
    }
}
